const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const os = require('os');
const threadLoader = require('thread-loader');
const webpackBar = require('webpackbar');

threadLoader.warmup({
  loader: 'thread-loader',
  options: {
    workers: 2, //默认是cpu个数-1
    // workerParallelJobs: 50, // 并行处理作业数，默认20
    poolRespawn: false,
    name: 'pool',
  }
},[
  'babel-loader',
])

module.exports = {
  entry: {
    main: path.resolve(__dirname, '../src/index.js')
  },
  resolve: {
    extensions: ['.js', '.json','.jsx', '.ts', '.tsx'], //导入模块可省略的后缀名，按数组顺序先后为查找顺序
    alias: { // 文件替代路基配置,&static在全局使用可引用static这个文件夹
      '&static': path.resolve(__dirname, '../src/static'),
      '&images': path.resolve(__dirname, '../src/static/images'),
      '&components': path.resolve(__dirname, '../src/components'),
      '&redux': path.resolve(__dirname, '../src/redux'),
      '&helpers': path.resolve(__dirname, '../src/helpers'),
      '&widgets': path.resolve(__dirname, '../src/widgets')
    }
  },
  module: {
    rules: [
      {
        test: /\.(eot|ttf|svg)$/,
        use: {
          loader: 'file-loader'
        } 
      },
      {
        test: /\.(js|jsx|ts|tsx)$/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'thread-loader',
            options: {
              workers: 2, //默认是cpu个数-1
              // workerParallelJobs: 50, // 并行处理作业数，默认20
              poolRespawn: false,
              name: 'pool',
            }
          },{
            loader: "babel-loader",
          }
        ]

      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, '../src/index.html'),
      title: 'test',
      hash: true, // css、js加入hash防止缓存
      inject: true, // js插入页面底部
    }),
    new CleanWebpackPlugin(),
    new webpackBar()
  ],
  optimization: {
    usedExports: true,
    splitChunks: {
      chunks: 'all', // 显示块的范围，3个可选值all, async, and initial，默认async（异步代码）
    },
  },
  output: {
    filename: 'js/[name].js',
    path: path.resolve(__dirname, '../dist')
  }
}